package com.armrest.screen.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

public class NestedRecyclerView extends RecyclerView {
    private static final String TAG = "MyHorizontalScrollView";
    private float mOffsetX, mOffsetY;
    private float mLastPosX, mLastPosY;

    public NestedRecyclerView(Context context) {
        super(context);
    }

    public NestedRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 根据手势滑动 处理是否给子控件处理
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result = false;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mOffsetX = 0.0F;
                mOffsetY = 0.0F;
                mLastPosX = ev.getX();
                mLastPosY = ev.getY();
                result = super.onInterceptTouchEvent(ev);
                break;
            default:
                float thisPosX = ev.getX();
                float thisPosY = ev.getY();
                mOffsetX += Math.abs(thisPosX - mLastPosX);//x偏移
                mOffsetY += Math.abs(thisPosY - mLastPosY);//y轴偏移
                mLastPosY = thisPosY;
                mLastPosX = thisPosX;
                if (mOffsetX < 3 && mOffsetY < 3)
                    result = false;
                else if (mOffsetY < mOffsetX)
                    result = true;//不传给子控件，自己水平滑动
                else
                    result = false;//传给子控件
                break;
         }
        return result;
    }
}